package com.lanhe.game.sysAppUser.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.utils.DateUtils;
import com.lanhe.common.utils.weixinUtils.WeiXinUtils;

import com.lanhe.game.sysAppUser.domain.SysAppUser;
import com.lanhe.game.sysAppUser.mapper.SysAppUserMapper;
import com.lanhe.game.sysAppUser.service.ISysAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * app用户登录Service业务层处理
 * 
 * @author tianliang
 * @date 2024-01-22
 */
@Service
public class SysAppUserServiceImpl extends ServiceImpl<SysAppUserMapper, SysAppUser> implements ISysAppUserService
{
    @Autowired
    private SysAppUserMapper sysAppUserMapper;

    @Autowired
    private WeiXinUtils weiXinUtils;


    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 查询app用户登录
     * 
     * @param appUserId app用户登录主键
     * @return app用户登录
     */
    @Override
    public SysAppUser selectSysAppUserByAppUserId(Long appUserId)
    {
        return sysAppUserMapper.selectSysAppUserByAppUserId(appUserId);
    }

    @Override
    public SysAppUser selectAppuserId(String openId) {
        return sysAppUserMapper.selectSysAppUserByOpenId(openId);
    }

    /**
     * 查询app用户登录列表
     * 
     * @param sysAppUser app用户登录
     * @return app用户登录
     */
    @Override
    public List<SysAppUser> selectSysAppUserList(SysAppUser sysAppUser)
    {
        return sysAppUserMapper.selectSysAppUserList(sysAppUser);
    }

    /**
     * 新增app用户登录
     * 
     * @param sysAppUser app用户登录
     * @return 结果
     */
    @Override
    public int insertSysAppUser(SysAppUser sysAppUser)
    {
        sysAppUser.setCreateTime(DateUtils.getNowDate());
        return sysAppUserMapper.insertSysAppUser(sysAppUser);
    }

    /**
     * 修改app用户登录
     * 
     * @param sysAppUser app用户登录
     * @return 结果
     */
    @Override
    public int updateSysAppUser(SysAppUser sysAppUser)
    {
        sysAppUser.setUpdateTime(DateUtils.getNowDate());
        return sysAppUserMapper.updateSysAppUser(sysAppUser);
    }

    /**
     * 更新会员基础余额
     *
     * @param appUserid 用户ID
     * @param totalAmount   金额
     * @author zhanggl
     * @date 2024/1/5
     */
    @Override
    public int updateLhMemberBalance(Long appUserid, BigDecimal totalAmount) {

        LambdaQueryWrapper<SysAppUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysAppUser::getAppUserId, appUserid);

        SysAppUser memberBase = sysAppUserMapper.selectById(appUserid);
        SysAppUser memberDb = new SysAppUser();


        memberDb.setTotalAmount(memberBase.getTotalAmount().subtract(totalAmount));
        if (ObjectUtil.isNull(memberBase.getLockAmount())) {
            memberDb.setLockAmount(totalAmount);
        } else {
            memberDb.setLockAmount(memberBase.getLockAmount().add(totalAmount));
        }

        return sysAppUserMapper.update(memberDb, lambdaQueryWrapper);
    }


    /**
     * 更新会员基础余额
     *
     * @param appUserid 用户ID
     * @param totalAmount   金额
     * @param type     类型
     * @author zhanggl
     * @date 2024/1/5
     */
    @Override
    public void updateMemberBaseBalance(Long appUserid, BigDecimal totalAmount, Integer type) {
        LambdaQueryWrapper<SysAppUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysAppUser::getAppUserId, appUserid);

        SysAppUser memberBase = sysAppUserMapper.selectById(appUserid);
        SysAppUser memberDb = new SysAppUser();

        // 如果拒绝，还原余额
        if (type.equals(Constants.ORDERS_WITHDRAWAL_STATUS_3)) {
            memberDb.setTotalAmount(memberBase.getTotalAmount().subtract(totalAmount));
        }
        memberDb.setLockAmount(memberBase.getLockAmount().subtract(totalAmount));
        sysAppUserMapper.update(memberDb, lambdaQueryWrapper);
    }

    /**
     * 批量删除app用户登录
     * 
     * @param appUserIds 需要删除的app用户登录主键
     * @return 结果
     */
    @Override
    public int deleteSysAppUserByAppUserIds(Long[] appUserIds)
    {
        return sysAppUserMapper.deleteSysAppUserByAppUserIds(appUserIds);
    }

    /**
     * 删除app用户登录信息
     * 
     * @param appUserId app用户登录主键
     * @return 结果
     */
    @Override
    public int deleteSysAppUserByAppUserId(Long appUserId)
    {
        return sysAppUserMapper.deleteSysAppUserByAppUserId(appUserId);
    }


}
